package com.monkey.apple.app.videoApp;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.monkey.apple.R;

import java.util.ArrayList;

/**
* Description: 视屏播放器app
* Author: Arthor
* Date: 2017/10/15
* Time: 8:43
*/

public class VideoActivity extends AppCompatActivity {

    private VideoUpdateTask videoUpdateTask;
    private ArrayList<VideoItem> mVideoList;
    private ListView listView;
    private VideoAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        videoUpdateTask = new VideoUpdateTask();
        videoUpdateTask.execute(this);

        mVideoList = new ArrayList<>();
        listView = ((ListView) findViewById(R.id.video_list));
        adapter = new VideoAdapter(this, R.layout.adapter_video_layout,mVideoList, LayoutInflater.from(this));
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });


    }

    /**
     * Activity退出的时候，判断VideoUpdateTask是否还在运行，如果还在运行，就让它停止
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoUpdateTask !=null && videoUpdateTask.getStatus() == AsyncTask.Status.RUNNING){
            videoUpdateTask.cancel(true);
        }
        videoUpdateTask = null;
    }


    class VideoUpdateTask extends AsyncTask<Object,VideoItem,Void> {

        @Override
        protected Void doInBackground(Object... params) {

            Context contexts = (Context) params[0];

            Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

            String[] searchKey = new String[]{
                    MediaStore.Video.Media.TITLE, //视频的标题
                    MediaStore.Images.Media.DATA, //视频的存放路径
                    MediaStore.Images.Media.DATE_ADDED //视频的创建时间
            };

            String[] keywords = null;
            String where = MediaStore.Video.Media.DATA + " like \"%"+"/Video"+"%\"";
            String sortOrder = MediaStore.Video.Media.DEFAULT_SORT_ORDER;

            ContentResolver resolver = contexts.getContentResolver();
            Cursor cursor = resolver.query(uri,null,null,null,null);
            if (cursor != null){
                while (cursor.moveToNext() && !isCancelled()){
                    String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                    String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));

                    String createdTime = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED));

                    VideoItem item = new VideoItem(path, name, createdTime);

                    publishProgress(item);
                }
                cursor.close();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(VideoItem... values) {
            super.onProgressUpdate(values);
            VideoItem item = values[0];
            Log.e("TAG", "onProgressUpdate:" + item.toString());
            mVideoList.add(item);
            adapter.notifyDataSetChanged();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }
    }
}
